package com.p2p.backend.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.p2p.backend.service.OplogService;
import com.p2p.backend.service.RealBackUserService;
import com.p2p.backend.service.RoleService;
import com.p2p.base.annotation.PermissionNeed;
import com.p2p.base.model.Action;
import com.p2p.base.model.Module;
import com.p2p.base.model.Oplog;
import com.p2p.base.util.MD5Util;
import com.p2p.base.web.controller.BaseController;

/**
 * leiwen@yiqihao.com
 */
@Controller
@RequestMapping("realuser")
public class RealUserController extends BaseController{

	@Resource
	private RealBackUserService realBackUserService;

	@Resource
	private RoleService roleService;

	@PermissionNeed("privilege_realuser_list")
	@RequestMapping("list")
	public void list(Model model) {
		model.addAttribute("res", 1);
		Map<String, Object> data = new HashMap<String, Object>();
		List<Map<String, Object>> list = realBackUserService.listAll();
		data.put("rows", list);
		data.put("total", list.size());
		model.addAttribute("data", data);
	}

	@PermissionNeed("privilege_realuser_management")
	@RequestMapping("doadd")
	public void doadd(@RequestParam Map<String, Object> params, Model model, HttpServletRequest request) {
		md5(params);
		realBackUserService.add(params);
		model.addAttribute("res", 1);
		Oplog oplog = super.oplog(request);
		oplog.setModule(Module.PRIVILEGE_USER);
		oplog.setAction(Action.ADD);
		oplog.setParams(ToStringBuilder.reflectionToString(params));
		OplogService.log(oplog);
	}

	@PermissionNeed("privilege_realuser_management")
	@RequestMapping("add")
	public String add(Model model) {
		initRole(model);
		return "realuser/add";
	}

	private void initRole(Model model) {
		List<Map<String, Object>> roleList = roleService.listAll();
		model.addAttribute("roleList", roleList);
	}

	@PermissionNeed("privilege_realuser_management")
	@RequestMapping("edit")
	public String edit(@RequestParam Integer id, Model model) {
		Map<String, Object> user = realBackUserService.getDetail(id);
		model.addAttribute("user", user);
		initRole(model);
		return "realuser/edit";
	}

	@PermissionNeed("privilege_realuser_management")
	@RequestMapping("doupdate")
	public void doupdate(@RequestParam Map<String, Object> params, Model model, HttpServletRequest request) {
		md5(params);
		realBackUserService.update(params);
		model.addAttribute("res", 1);
		Oplog oplog = super.oplog(request);
		oplog.setModule(Module.PRIVILEGE_USER);
		oplog.setAction(Action.UPDATE);
		oplog.setParams(ToStringBuilder.reflectionToString(params));
		OplogService.log(oplog);
	}

	private void md5(Map<String, Object> params) {
		String key = "password";
		String password = MapUtils.getString(params, key);
		if (StringUtils.isNoneBlank(password)) {
			String mdPassword = MD5Util.sha1(password);
			params.put(key, mdPassword);
		}else{
			params.remove(key);
		}
	}
}
